# -*- coding: utf-8 -*-
import wx
import wx.xrc
from core.file import File
from gui.hint_dialogs import ErrorDialog, WarmingDialog


class NewFileDialog(wx.Dialog):

    def __init__(self, parent, suffix: str, file:File, root_file, is_folder=False):
        wx.Dialog.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
                           size=wx.Size(300, 150), style=wx.DEFAULT_DIALOG_STYLE)
        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
        self.is_folder = is_folder
        if not is_folder:
            suffix = "text" if not suffix else suffix
            suffix = suffix.replace(" ", "").replace("\t", "").replace(".", "")
            suffix = "text" if len(suffix) == 0 else suffix
            suffix = f".{suffix}"
        else:
            suffix = ""
        self.suffix = suffix
        self.file = file
        self.root_file = root_file
        bSizer11 = wx.BoxSizer(wx.VERTICAL)

        bSizer12 = wx.BoxSizer(wx.HORIZONTAL)

        self.textCtrl = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(230, -1), 0)
        bSizer12.Add(self.textCtrl, 0, wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, 5)

        self.m_staticText5 = wx.StaticText(self, wx.ID_ANY, self.suffix, wx.DefaultPosition, wx.Size(50, -1),
                                           wx.ALIGN_LEFT)
        self.m_staticText5.Wrap(-1)

        bSizer12.Add(self.m_staticText5, 0, wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, 5)

        bSizer11.Add(bSizer12, 1, wx.EXPAND, 5)

        self.b_submit = wx.Button(self, wx.ID_ANY, u"确认", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer11.Add(self.b_submit, 0, wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, 5)

        self.SetSizer(bSizer11)
        self.Layout()

        self.Centre(wx.BOTH)
        self.b_submit.Bind(wx.EVT_BUTTON, self.submit)

    def __del__(self):
        pass

    def submit(self, event):
        value = self.textCtrl.Value
        if not value:
            WarmingDialog(self, "请输入文件名之后提交").Show()
        elif len(value.replace(" ", "").replace("\t", "")) == 0:
            WarmingDialog(self, "文件名称中不能包含空格、和制表符").Show()
        else:
            self.file.new(f"{value}{self.suffix}", is_folder=self.is_folder)
            WarmingDialog(self, "文件创建成功").Show()
        self.Close()
        self.Parent.refresh(self.root_file)
